<?php

class User extends CI_Model
{
  public $user_id;
  public $user_mail;
  public $user_password;

  public function __construct()
  {
    parent::__construct();
  }

  public function load($id)
  {
    $query = $this->db->get_where('user', array('user_id' => $id));
    $this->user_id = -1;
    foreach ($query->result() as $row)
      {
	$this->user_id = $row->user_id;
	$this->user_mail = $row->user_mail;
	$this->user_password = $row->user_password;
	break;
      }
    if ($this->user_id != -1)
      return true;
    return false;
  }

  public function insert()
  {
    $data = array(
		  'user_mail' => $this->user_mail,
		  'user_password' => $this->user_password
		  );

    $this->db->insert('user', $data); 
  }

  public function getIdByMail($mail)
  {
    $query = $this->db->get_where('user', array('user_mail' => $mail));
    foreach ($query->result() as $row)
      return $row->user_id ;
    return -1;
  }
  
  public function checkLogin($mail, $password)
  {
    $id = $this->getIdByMail($mail);
    if ($id == -1)
      return false;
    if (!$this->load($id))
      return false;
    if ($password != $this->user_password)
      return false;
    return $id;
  }
}